<template>
  <div class="scroll-view-container">
    <!-- 左边一级分类 -->
    <div class="left-scroll-view">
      <!-- 参考：https://v2.cn.vuejs.org/v2/guide/class-and-style.html -->
      <!-- <div
        class="left-scroll-view-item"
        :class="{ active: currentIndex === index }"
        v-for="(item, index) in categories"
        :key="item.cat_id"
        @click="toggle(index)"
      > -->
      <div
        :class="[
          'left-scroll-view-item',
          currentIndex === index ? 'active' : ''
        ]"
        v-for="(item, index) in categories"
        :key="item.cat_id"
        @click="toggle(index)"
      >
        {{ item.cat_name }}
      </div>
    </div>
    <!-- 右边二三级分类 -->
    <div v-if="level2List && level2List.length > 0" class="right-scroll-view">
      <!-- 二级标题 -->
      <div v-for="item in level2List" :key="item.cat_id" class="cate-lv2-title">
        <h4>{{ item.cat_name }}</h4>
        <!-- 三级分类 -->
        <div class="cate-lv3-list">
          <div
            v-for="subitem in item.children"
            :key="subitem.cat_id"
            class="cate-lv3-item"
          >
            <img :src="subitem.cat_icon" alt="" />
            <span class="name">{{ subitem.cat_name }}</span>
          </div>
        </div>
      </div>
    </div>
    <div v-else>
      <van-empty description="没有二级分类~" />
    </div>
  </div>
</template>

<script setup name="Category">
import { ref, computed } from 'vue'
import { getCategories } from '@/api/category'

// 定义模型数据
const categories = ref([])
const currentIndex = ref(0)

const getCategoriesData = async () => {
  const res = await getCategories()
  categories.value = res.data
}

// 注意children的取值
const level2List = computed(
  () =>
    categories.value[currentIndex.value] &&
    categories.value[currentIndex.value].children
)

// 切换一级分类
const toggle = index => {
  currentIndex.value = index
}

getCategoriesData()
</script>

<style lang="scss" scoped>
.scroll-view-container {
  display: flex;
  height: 100%;

  .left-scroll-view {
    width: 120px;
    height: calc(100vh - 52px);
    overflow: auto;

    .left-scroll-view-item {
      line-height: 60px;
      background-color: #f7f7f7;
      text-align: center;
      font-size: 12px;

      // 激活项的样式
      &.active {
        background-color: #ffffff;
        position: relative;
        color: #c00000;
      }
    }
  }

  // 去除滚动条
  .left-scroll-view::-webkit-scrollbar {
    width: 0 !important;
  }

  .right-scroll-view {
    height: calc(100vh - 52px);
    overflow: auto;
    flex: 1;
    .cate-lv2-title {
      font-size: 12px;
      font-weight: bold;
      padding: 0 15px;
    }
    .cate-lv3-list {
      display: flex;
      flex-wrap: wrap;

      .cate-lv3-item {
        width: 33.33%;
        margin-bottom: 10px;
        display: flex;
        flex-direction: column;
        align-items: center;

        img {
          width: 60px;
          height: 60px;
        }

        .name {
          font-size: 12px;
        }
      }
    }
  }

  // 去除滚动条
  .right-scroll-view::-webkit-scrollbar {
    width: 0 !important;
  }
}
</style>
